use std::{cmp::Ordering, io::{self, stdout, BufWriter}};
use ferris_says::say;
use rand::Rng;

pub fn entry() {
    ferris_say("猜数字!");

    let secret_number = rand::thread_rng().gen_range(1..=100);
    // println!("debug secret_number={secret_number}");
    
    loop {
        println!("答案的范围是[1-100], 请输入你猜测的数字。");

        let mut guess =  String::new();
        io::stdin()
            .read_line(&mut guess)
            .expect("读取输入失败！");
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };

        match guess.cmp(&secret_number) {
            Ordering::Equal => {
                println!("猜对啦！");
                break;
            },
            Ordering::Less => println!("太小了"),
            Ordering::Greater => println!("太大了"),
        }
    }
}

pub fn ferris_say(msg: &str) {
    let stdout = stdout();
    let message = String::from(msg);
    let width = message.chars().count();

    let mut writer = BufWriter::new(stdout.lock());
    say(&message, width, &mut writer).unwrap();
}
